package com.gfm.story.service.dict;


import com.gfm.story.dao.DictionaryDao;
import com.gfm.story.dao.DictionaryDetailDao;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.dict.Dictionary;
import com.gfm.story.entity.dict.DictionaryDetail;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据字典
 * @Author: ly
 * @CreateDate: 2019/3/7 0007 下午 4:23
 * @UpdateUser: yc
 * @UpdateDate: 2019/3/7 0007 下午 4:23
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class DictionaryService {

    @Resource
    private DictionaryDao sysDictionaryDao;


    @Resource

    private DictionaryDetailDao sysDictionaryDetailDao;

    /**
     * 获取数据字典信息列表页
     *
     * @param map
     * @return
     */
    public ResultBean getSysDictionaryPage(Map<String, Object> map) {
        List<Map> list = this.sysDictionaryDao.selectSysDictionaryPage(map);
        int count = this.sysDictionaryDao.selectSysDictionaryPageCount(map);
        JSONObject json = new JSONObject();
        json.put("total", count);
        json.put("objs", list);
        return ResultBean.getSuccess(json);

    }

    /**
     * 新增或者修改父级数据字典
     *
     * @param sysDictionary
     * @return
     */
    public ResultBean addOrEdit(Dictionary sysDictionary) {
        if (null != sysDictionary) {
            Dictionary db = null;
            try {
                Dictionary param = new Dictionary();
                param.setDictCode(sysDictionary.getDictCode());
                db = this.sysDictionaryDao.selectCode(param);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultBean.getError("服务器开小差!");
            }
            /**
             * 新增或者修改的时候创建时间,修改时间以及默认删除状态不可修改
             */
            sysDictionary.setIsDelete(null);
            sysDictionary.setCreateTime(null);
            sysDictionary.setUpdateTime(null);
            Long userId = new Long(1);
            /**
             * 前端页面传入数据字典的ID为空则表示新增
             */
            if (sysDictionary.getId() == null) {
                /**
                 * 根据按钮唯一Code查找是否已经存在
                 */
                if (db != null) {
                    return ResultBean.getError("数据字典已存在!");
                }
                sysDictionary.insert();
                return ResultBean.getSuccess("操作成功!");

            } else {
                /**
                 * 如果ID为空则为编辑
                 * 数据字典唯一编码不能修改
                 */
                sysDictionary.setDictCode(null);
                Dictionary sysDictionaryOldData = sysDictionary.selectById();
                sysDictionary.updateById();
                Dictionary sysDictionaryNewData = sysDictionary.selectById();
                return ResultBean.getSuccess("操作成功!");
            }
        } else {
            return ResultBean.getError("参数为空");
        }


    }

    /**
     * 删除数据字典
     *
     * @param id
     * @return
     */
    public ResultBean remove(Long id) {
        if (StringUtils.isNotBlank(id.toString())) {
            /**
             * 判断是否有数据字典列表信息，如果有需要一起关联删除
             */
            Map<String, Object> map = new HashMap<>();
            map.put("dictId", id);
            List<Map> mapList = sysDictionaryDetailDao.selectSysDictionaryDetailList(map);
            for (Map detailMap : mapList) {
                DictionaryDetail sysDictionaryDetail = new DictionaryDetail();
                sysDictionaryDetail.deleteById(Long.valueOf(detailMap.get("id").toString()));
            }
            Dictionary sysDictionary = new Dictionary();
            sysDictionary.setId(id);
            sysDictionary.deleteById();

            return ResultBean.getSuccess("删除成功!");
        } else {
            return ResultBean.getError("参数为空");
        }

    }
}